package com.hl.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hl.dao.CommunityDao;
import com.hl.dao.CommunityDaoImpl;
import com.hl.pojo.PanghuCommunity;
import com.hl.util.GeneralResult;
import com.hl.util.HttpUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/community/communityUpdate")
public class CommunityUpdate extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取参数字符串（将io流转为json字符串）
        String json = HttpUtil.getJson(req);
        //将json字符串转为java对象
        PanghuCommunity community = JSONObject.parseObject(json, PanghuCommunity.class);
        CommunityDaoImpl dao = new CommunityDaoImpl();
        int num = 0;
        //判断是新增还是修改
        if (community!=null && community.getCommunityId()!=null && !community.getCommunityId().equals("")){
            //有值，修改
            num = dao.update(community);
        }else {
            //没有值，新增
            num = dao.save(community);
        }
        String msg = num >0?"操作成功":"操作失败";
        //将结果返回给前端
        resp.getWriter().append(JSON.toJSONString(new GeneralResult(200,msg)));

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
